package com.lgkj.ui.manager;

import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.google.gson.Gson;
import com.lgkj.R;
import com.lgkj.adapter.HairSalonAdapter;
import com.lgkj.adapter.ShopAdAdapter;
import com.lgkj.bean.ShopAdBean;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;
import com.lgkj.utils.TitleBuilder;
import com.lgkj.view.SelectPicPopupWindow;

import org.devio.takephoto.app.TakePhotoActivity;
import org.devio.takephoto.model.TResult;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/7/7 0007.
 */

public class ShopAdActivity extends TakePhotoActivity {
    @BindView(R.id.gv_list)
    GridView gvList;
    private ShopAdAdapter adapter;
    private ShopAdBean dataBean;
    private SelectPicPopupWindow menuWindow;
    private String imageId;

    @Override
    public int getLayoutId() {
        return R.layout.activity_shop_ad;
    }

    @Override
    public void initView() {
        new TitleBuilder(this).setTitleText("本店广告").setLeftIcoListening(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        adapter = new ShopAdAdapter(ShopAdActivity.this);
        gvList.setAdapter(adapter);
        initDatas();
    }

    @Override
    public void initData() {
        adapter.setClick(new HairSalonAdapter.MyClickListener() {
            @Override
            public void clickListener(View v) {
                try {
                    int id = Integer.parseInt(v.getTag().toString());
                    del(adapter.getItem(id).getId());
                } catch (Exception e) {
                    Log.e(TAG, "clickListener: " + e.getMessage());
                }

            }
        });
        gvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if ((i + 1) == adapter.getCount()) {
                    menuWindow = new SelectPicPopupWindow(ShopAdActivity.this, itemsOnClick);
//        menuWindow.sethideTake();
                    //显示窗口
                    menuWindow.showAtLocation(ShopAdActivity.this.findViewById(R.id.main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                } else {
                    //放大
//                    Intent intent = new Intent(ShopAdActivity.this, ImageBrowseActivity.class);
//                    intent.putExtra("image_url", adapter.getItem(i).getUrl());
//                    startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(ShopAdActivity.this, gvList, "shareNames").toBundle());
                }
            }
        });
    }

    private View.OnClickListener itemsOnClick = new View.OnClickListener() {

        public void onClick(View v) {
            menuWindow.dismiss();
            switch (v.getId()) {
                case R.id.btn_take_photo:
                    CommonUtils.takeFromCapture(getTakePhoto(), 864, 480);
                    break;
                case R.id.btn_pick_photo:
                    CommonUtils.takeFromDocuments(getTakePhoto(), 864, 480);
                    //调用系统图库的意图
                    break;
                default:
                    break;
            }
        }
    };


    @Override
    public void takeCancel() {
        super.takeCancel();
    }

    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        String url = CommonConstant.getTakeImagePath();
        Log.i(TAG, "showImg: " + url);
        upload(new File(url));
//        Glide.with(ShopAmbientActivity.this).load(new File(url)).into(iv_img);
    }

    private void upload(File file) {
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(ShopAdActivity.this));
        HttpUtils.uploadImage(map, CommonConstant.uploadImg, file, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                toastLong("上传成功", 0);
                imageId = jsonObject.optString("id");
                save();
            }

            @Override
            public void errorHttp(String msg) {
                toastLong("上传失败", 0);
            }
        });
    }

    private void save() {
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(ShopAdActivity.this));
        map.put("barbershop_id", CommonUtils.getShopId(ShopAdActivity.this));
        map.put("dm_img_id", imageId);
        HttpUtils.postHttp(map, CommonConstant.barbershopdmadd, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                toastLong(jsonObject.optString("msg"), 0);
                if (jsonObject.optInt("code") == 0) {
                    initDatas();
                }
            }

            @Override
            public void errorHttp(String msg) {

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void initDatas() {
        showDiaLog("正在获取数据");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(ShopAdActivity.this));
        map.put("barbershop_id", CommonUtils.getShopId(ShopAdActivity.this));
        HttpUtils.postHttp(map, CommonConstant.barbershopdmlist, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("code") == 0) {
                    Gson mGson = new Gson();
                    dataBean = mGson.fromJson(String.valueOf(jsonObject), ShopAdBean.class);
                    if (dataBean != null) {
                        adapter.setData(dataBean.getData());
                        adapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }

    private void del(String id) {
        showDiaLog("正在删除数据");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(ShopAdActivity.this));
        map.put("id", id);
        HttpUtils.postHttp(map, CommonConstant.barbershopdmdel, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("code") == 0) {
                    initDatas();
                }
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }
}
